package com.caine.tc.server.handler;

import com.caine.tc.tool.JSONTool;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.channel.group.ChannelGroup;
import io.netty.channel.group.DefaultChannelGroup;
import io.netty.util.concurrent.GlobalEventExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author: CaineZhu
 * @Description:
 * @Date: Created in 11:10 2020/11/24
 * @Modified By:
 */
public class ChildChannelHandler extends SimpleChannelInboundHandler {

    Logger log = LoggerFactory.getLogger(this.getClass());

    private static ChannelGroup channelGroup = new DefaultChannelGroup(GlobalEventExecutor.INSTANCE);
    private static Map<String, List<String>> transationIdMap = new ConcurrentHashMap<>();



    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
       /* String message = msg.toString();
        log.info(">>>>>>>>>>接收数据:{}", message);
        Map<String, Object> objMap = JSONTool.obj2Map(message);
        String command = objMap.getOrDefault("command", "").toString();
        String groupId = objMap.getOrDefault("groupId", "").toString();
        String transactionType = objMap.getOrDefault("transactionType", "").toString();
        String transactionId = objMap.getOrDefault("transactionId", "").toString();
        if ("create".equals(command)) {
            transationIdMap.put(groupId, new ArrayList<>());
        }else if("regist".equals(command)){
            transationIdMap.get(groupId).add(transactionId);
            if("rollback".equals(command)) {
                log.info(">>>>>>接收到回滚消息");
                sendMsg(groupId, "rollback");
            }
        }else if("commit".equals(command)){
            log.info(">>>>>>全局事务提交");
        }*/
        ByteBuf buf = Unpooled.compositeBuffer(1);
        buf.writeByte(0x09);
        ctx.channel().writeAndFlush(buf);
    }

    private void sendMsg(String groupId, String command){
            ByteBuf buf = Unpooled.compositeBuffer();
            buf.writeByte(0x23);
            buf.writeByte(0x23);
            buf.writeByte(0xff); // 命令标识
            buf.writeByte(0xff);// 应答标识
            byte[] vin = new byte[17];
            buf.writeBytes(vin);
            buf.writeByte(0x01);// 数据单元加密方式
            short len = 0;
            buf.writeShort(len);// 数据单元长度
            byte bcc = (byte)0x00;
            buf.writeByte(bcc);
    }

    private void structWrap(ByteBuf buf,byte[] vin,byte[] data){
        buf.writeByte(0x23);
        buf.writeByte(0x23);
        buf.writeByte(0xff); // 命令标识
        buf.writeByte(0xff);// 应答标识
//        byte[] vin = new byte[17];
        buf.writeBytes(vin);
        buf.writeByte(0x01);// 数据单元加密方式
        short len = 0;
        buf.writeShort(len);// 数据单元长度
        buf.writeBytes(data);
        byte bcc = bcc(data);
        buf.writeByte(bcc);
    }

    private static byte bcc(byte[] data) {
        byte bcc = 0x00;
        for (int i = 0; i < data.length; i++) {
            bcc = (byte) (bcc ^ data[i]);
        }
        return bcc;
    }


    public static void main(String[] args) {
//        byte bcc = bcc(new byte[]{0x01, 0x02, 0x03});
//        System.out.println(Integer.toHexString(bcc));
//        byte b = (byte)Integer.parseUnsignedInt("A8",16);
//        System.out.println(Integer.toHexString(b & 0xff));
        String hex1 = "0637d7c5";
        String hex2 = "01d6ceb1";
        Integer integer1 = Integer.valueOf(hex1, 16);
        Integer integer2 = Integer.valueOf(hex2, 16);
        System.out.println(integer1);
        System.out.println(integer2);


    }


}
